while practice¶

Learning Outcomes

  • nested while
  • more complex decomposition (especially cover and climb)
    • recognizing patterns and constructing loops
    • boundary conditions: how do the pieces fit together?

Fix the bridge¶

fix_bridge.py¶

The bridge over the river has been washed out. Fix it!

NOTES

This problem is a simple, three-loop problem with slightly more complicated glue code (e.g. shifting down to the red square from the row above).

Give the students opportunities to discuss the problem and map it out with a partner.

Show how each stage of the problem can be tested before moving on to the next stage.

Observe how starting on a red square colludes with a color-based loop condition, requiring that bit move of the square first. Or (as done in the solution), you can simply use bit.front_clear().

Explain that an important skill in programming is identifying the technical requirements of a problem. Often, when the problem is simple, the solution can be simple. If the problem changes to become more complex, the code will need to change also. Comparing across instances of a problem helps us understand the nature of the problem and decide how complicated the code needs to be.

For example, if one of the bridges had an empty space immediately after the bridge, bit.front_clear() would not work, and you'd have to use a color-based loop condition. Add fix-different-bridge to the script to demonstrate.

Talk through how the code needs to change to adapt to this new requirement.

The next two problems introduce the important concept of nested loops, so don't spend too much time on this one (maybe 10-15 minutes, leaving more time for the other problem).

Code is almost always intended to run on many different inputs. Compare across possible inputs to determine the technical requirements of your code.

There is always a compromise: code that handles more kinds of inputs is more complex; code that handles fewer, specific inputs is more simple.

Good rule of thumb: make your code only as complicated as it needs to be.

🧑🏼‍🎨 Blue Ocean¶

blue_ocean.py¶

Fill the world with blue.

Bit gives us move, paint, etc.

What new verbs would make this job even easier?

NOTES

We've done problems like this before (nested while, etc.), but this is the first time we are using functions to define the pieces. The emphasis of this exercise is on the decomposition and abstraction of the parts, and then putting those parts together.

Several strategies to consider:

  • Row-by-row, zig-zag
  • Row-by-row, down-and-back
  • Column-by-column
  • Spiral

Which seems easiest? Why?

  • Computers (and humans!) don't do well will uncertainty. We like strategies with 0% uncertainty.
  • When we loop through a block of code, we want to know exactly where Bit will be before and after each iteration.
  • When we call a function, we want to know exactly where Bit will be before and after the function call.

Activity

  • implement go using fill_row_with_blue(bit) (even though it isn't defined yet)
  • Then define and implement fill_row_with_blue

  • What are the boundary conditions of fill_row_with_blue?

    • Where does bit start?
    • Where does bit end?
    • Notice how the prep (turn up) and the down-and-back make the problem easier

Remember¶

  • Choose strategies that minimize uncertainty
  • Pick concise but accurate and helpful names for your functions
  • Document the purpose and boundary conditions of the function
    • What does the function accomplish?
    • Where does Bit start? What direction is he facing?
    • Where does Bit end? What direction is he facing?

Key ideas¶

  • Identifying technical requirements by comparing inputs to a problem
  • Practice decomposing complex problems